const jwt = require('jsonwebtoken')

const {JWT_SECRET} = require('../config/config.default')

const {
	tokenExpiredError,
	invalidToken,
	noPermission
} = require('../constant/err.type')


const auth = async(ctx,next)=>{
	const { authorization = '' } = ctx.request.header
	const token = authorization.replace('Bearer ','')
	// console.log(token);
	try {
		const user = jwt.verify(token, JWT_SECRET)
		ctx.state.user = user
	} catch (err) {
		switch (err.name) {
			case 'TokenExpiredError':
				console.error('token已过期', err)
				return ctx.app.emit('error', tokenExpiredError, ctx)
			case 'JsonWebTokenError':
				console.error('无效的token', err)
				return ctx.app.emit('error', invalidToken, ctx)
		}
	}
	await next()
}

const isAdmin = async(ctx,next)=>{
	const {is_admin} = ctx.state.user
	if(!is_admin){
		return ctx.app.emit('error',noPermission,ctx)
	}
	await next()
}


module.exports = {
	auth,
	isAdmin
}